package com.example.ex2.controllers;

import com.example.ex2.mappers.StudentMapper;
import com.example.ex2.pojo.Course;
import com.example.ex2.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
public class CourseSelectController {
    @GetMapping("courseSelect")
    public String courseSelect(){
        return "courseSelect";
    }

    StudentMapper studentMapper = new StudentMapper();
    @RequestMapping("courseSelect/submit")
    public String personalInformationSubmit(@RequestParam(value = "course1",required = false) String courseName1,
                                            @RequestParam(value = "course2",required = false) String courseName2,
                                            @RequestParam(value = "course3",required = false) String courseName3,
                                            @RequestParam(value = "course4",required = false) String courseName4,
                                            @RequestParam(value = "course5",required = false) String courseName5,
                                            Model model){


        Map map = new HashMap<String,Student>();
        model.addAttribute("Information",studentMapper.show(map));
        ArrayList<Course> list = new ArrayList<>();
        if (courseName1 != null){
            list.add(new Course(courseName1));
        }
        if (courseName2 != null){
            list.add(new Course(courseName2));
        }
        if (courseName3 != null){
            list.add(new Course(courseName3));
        }
        if (courseName4 != null){
            list.add(new Course(courseName4));
        }
        if (courseName5 != null){
            list.add(new Course(courseName5));
        }

        model.addAttribute("courseSelected",list);
        return "result";
    }


}
